package cn.mrcode.smartPluginSecurity.helper;

import cn.mrcode.smartPluginSecurity.exception.AuthcException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

/**
 * @author zhuqiang
 * @version V1.0
 * @date 2015/11/14 23:34
 */
public class SecurityHelper {
    /**
     * 登录
     * @param username
     * @param password
     * @throws AuthcException
     */
    public static void login(String username,String password) throws AuthcException{
        Subject subject = SecurityUtils.getSubject();
        if(subject != null){
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            try{
                subject.login(token);
            }catch (AuthenticationException e) {
                throw new AuthcException(e);
            }
        }
    }
    /** 登出 **/
    public static void logout(){
        Subject subject = SecurityUtils.getSubject();
        if(subject != null){
            subject.logout();
        }
    }
}
